<?php
global $mySession;
?>
<link href="<?= CSS_URL ?>style2.css" rel="stylesheet">
<style type="text/css">
    #map_canvas
    {
        width:100%;height:300px;border: solid 1px #cccccc;	
        margin-left:15px;
    }
    .check_bx .checkbox_block
    {
        width:50%;
        float:left;
    }
    .check_bx .checkbox_block > input
    {
        float:left;
    }

    .check_bx .checkbox_block > label
    {
        float:left;
    }

    .rental_rate_blck label {
        color: #FFFFFF;
        font-size: 13px;
        font-weight: bold;
        padding-left: 131px;
        padding-top: 30px;
    }


    .extras_blck label[title="from"] {
        color: #FFFFFF;
        padding-left: 103px;
    }
    .extras_blck label {
        color: #FFFFFF;
        font-size: 13px;
        font-weight: bold;
        padding-left: 135px;
        padding-top: 30px;
    }
</style>
<script>
    var SITEURL = "<?= APPLICATION_URL; ?>";
    var PPTY_ID = "<?= $this->pptyId ?>";
    var dataArray = [];
</script>
<?
if ($this->step == '3') {
    ?>
    <script type="text/javascript" src="<?= JS_URL ?>main.js"></script>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>
    <?
}
?>
<link href="<?= CSS_URL ?>style.css" rel="stylesheet">
<script type="text/javascript">
    $(document).ready(function(e) {

        $("#check_in").datepicker({
            maxDate: new Date(),
            dateFormat: "dd/mm/yy"
        });

        jQuery.validator.addMethod("validUrl", function(value, element) {
            return this.optional(element) || /^(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);
        }, "Please Enter Valid URL");

        $("#mws-ui-button-radio").buttonset();
    });

    function do_save_cal()
    {

        if ($("#mws-validate").valid())
        {
            $.ajax({
                type: 'POST',
                url: '<?= APPLICATION_URL_ADMIN ?>property/savecalendarstat/pptyId/<?= $this->pptyId ?>',
                async: false,
                data: ({Datef: $("#date_from").val(), Datet: $("#date_to").val(), Status: $("#status").val()}),
                success: function(data) {
                    //alert(data);
                    window.location = window.location.href;

                }

            });

        }
        return false;
    }

    //upload image code ends	
    //currency function
    function setcurrency()
    {
        if ($("#currency").val() != "")
        {

            $.ajax({
                type: 'POST',
                url: '<?= APPLICATION_URL_ADMIN ?>property/setcurrency/pptyId/<?= $this->pptyId ?>',
                async: false,
                data: ({Value: $("#currency").val()}),
                success: function(data) {
                    data = $.trim(data);
                    window.location = window.location.href;

                }
            });
        }

    }

</script>
<!-- scripts for rental rates-->
<script type="text/javascript">
    function checkvalid(id)
    {
        var error_flag = 0;
        $("#error_discount_offer" + id).hide();
        $("#error_valid_from" + id).hide();
        $("#error_valid_to" + id).hide();
        $("#error_min_stay" + id).hide();
        $("#error_bookby" + id).hide();

        if ($("#discount_offer" + id).length > 0)
        {
            if (isNaN($('#discount_offer' + id).val()) || $('#discount_offer' + id).val() == "" || $('#discount_offer' + id).val() == null || $("#discount_offer" + id).val().length > 3)
            {
                $("#error_discount_offer" + id).show();
                error_flag = 1;
            }
        }

        if ($("#valid_from" + id).val() == "" || $("#valid_from" + id).val() == null)
        {
            $("#error_valid_from" + id).show();
            error_flag = 1;
        }

        if ($("#valid_to" + id).val() == "" || $("#valid_to" + id).val() == null)
        {
            $("#error_valid_to" + id).show();
            error_flag = 1;
        }

        if ($("#min_stay" + id).length > 0)
        {
            if (isNaN($("#min_stay" + id).val()) || $("#min_stay" + id).val() == "" || $("#min_stay" + id).val() == null || $("#min_stay" + id).val().length > 3)
            {
                $("#error_min_stay" + id).show();
                error_flag = 1;
            }
        }

        if ($("#bookby" + id).val() == "" || $("#bookby" + id).val() == null)
        {
            $("#error_bookby" + id).show();
            error_flag = 1;
        }



        if (error_flag == 0)
            $.ajax({
                url: "<?= APPLICATION_URL_ADMIN ?>updater/saveoffers/pptyId/<?= $this->pptyId ?>",
                type: "POST",
                data: ({Valid_f: $("#valid_from" + id).val(), Valid_t: $("#valid_to" + id).val(), Nights: $("#min_stay" + id).val(), Discount: $("#discount_offer" + id).val(), Book_by: $("#bookby" + id).val(), offer_id: $("#offer_id" + id).val()}),
                async: false,
                success: function(data) {
                    location.reload();
                }


            });

    }

    function dactive(id)
    {
        var c = confirm("Are you sure you want to de-activate this offer");
        if (c)
        {
            $.ajax({
                url: "<?= APPLICATION_URL_ADMIN ?>property/deactivateoffers/id/" + $("#spcl_offer_id" + id).val(),
                type: "GET",
                async: false,
                success: function(data) {
                    data = $.trim(data);
                    window.location.href = "<?= APPLICATION_URL_ADMIN ?>property/editproperty/pptyId/<?= $this->pptyId ?>/step/6";
                }


            });
        }
    }

    function mindate(value, id)
    {
        var selectedDate = new Date(value);
        var msecsInADay = 86400000;
        var endDate = new Date(selectedDate.getTime() + msecsInADay);
        $("#" + id).datepicker("option", "minDate", endDate);
    }
    function mindateforbook(value, id)
    {
        var selectedDate = new Date(value);
        var msecsInADay = 86400000;
        var endDate = new Date(selectedDate.getTime() - msecsInADay);
        $("#" + id).datepicker("option", "maxDate", endDate);
    }

    function do_save_ppty() //function for saving rental rates
    {
        var flag = 0;
        if ($("#mws-validate1").valid())
        {
            var Data = 0;
            $('#rate_blck').html("<div align = 'center' style='width:100%;'><img src = '<?= IMAGES_URL ?>black-001-loading.gif' width = '20' height = '20'></div>");
            $.ajax({
                url: "<?= APPLICATION_URL_ADMIN ?>updater/setrates/pptyId/<?= $this->pptyId ?>",
                type: "POST",
                data: ({Date_f: $("#date_from").val(), Date_t: $("#date_to").val(), Nights: $("#nights").val(), Rate: $("#rate").val(), Option: $('.rateOption:checked').val()}),
                async: false,
                success: function(data) {

                    data = $.trim(data);
                    flag = 1;
                    do_ppty();
                }
            })
            return false;
        }

        if (flag == 1)
            do_ppty();
    }

    function do_ppty() //function for displaying rental rates
    {
        var Data = 0;
        $('#rate_blck').html("<div align = 'center' style= 'clear:both;width:100%;'><img src = '<?= IMAGES_URL ?>black-001-loading.gif' width = '20' height = '20'></div>");

        $.ajax({
            url: "<?= APPLICATION_URL_ADMIN ?>property/getrates/pptyId/<?= $this->pptyId ?>",
            type: "GET",
            async: false,
            success: function(data) {

                data = $.trim(data);
                $("#rate_blck").text("");

                if (data != '0' && data != "" && data != null)
                {
                    var ArrData = data.split("|");
                    if (ArrData.length > 0)
                    {
                        for (var i = 0; i < ArrData.length; i++)
                        {
                            var mySplit = ArrData[i].split(",");
                            var OptionValue1 = mySplit[0];
                            var OptionValue2 = mySplit[1];
                            var OptionValue3 = mySplit[2];
                            var OptionValue4 = mySplit[3];
                            var OptionValue5 = mySplit[4];
                            $("#rate_blck").append("<div style='width:100%;float:left;'><div style='width:20%' align = 'center'><label>" + OptionValue1 + "</label></div><div style='width:20%;' align = 'center'><label >" + OptionValue2 + "</label></div><div style='width:30%;' align='center'><label>" + OptionValue3 + "</label></div><div style='width:23%;' align='center'><label>" + OptionValue4 + "</label></div><div onclick = 'modifyrate(" + i + ",&quot;" + OptionValue5 + "&quot;,&quot;" + OptionValue1 + "&quot;,&quot;" + OptionValue2 + "&quot;,&quot;" + OptionValue3 + "&quot;,&quot;" + OptionValue4 + "&quot;);' style = 'cursor:pointer'><img class='deletes' src='<?= IMAGES_URL ?>application_form_edit.png' width = '20' height = '20'></div><div onclick = 'deleterate(" + OptionValue5 + ");' style = 'cursor:pointer'><img class='deletes' src='<?= IMAGES_URL ?>delete.png' width = '20' height = '20'></div></div>");

                            if (i + 1 == ArrData.length)
                                $("#cal_date").val(OptionValue2);

                        }


                    }


                }
                else
                {
                    $("#rate_blck").html("<div align='center' style = 'color:red;width:100%;'>No Items to display</div>");
                    $("#cal_date").val("");
                }
            }

        })

        return false;
    }


    function ratetocheck(ptr)
    {
        var rateto = ptr.value.split("")
        $("#rateFrom").datepicker("option", "maxDate", new Date(ptr.value));
    }

    function ratefromcheck(ptr)
    {
        var rateto = ptr.value.split("")
        $("#rateTo").datepicker("option", "minDate", new Date(ptr.value));
    }

    function modifyrate(i, id, from, to, night, rate)
    {
        var MIN_NIGHTS = "<select id = 'minNights'>";

        for (var j = 7; j <= 14; j++)
        {
            if (j == night)
            {
                var selected = 'selected="selected"'
            }
            else
            {
                var selected = '';
            }
            MIN_NIGHTS += "<option " + selected + ">" + j + "</option>";

        }
        MIN_NIGHTS += "</select>";

        $('#rate_blck').children("div :eq(" + i + ")").html("<div style='width:100%;float:left;margin-top:10px;' ><div style='width:20%' align = 'center'><label>" + from + "</label></div><div style='width:20%;' align = 'center'><label >" + to + "</label></div><div style='width:30%;' align='center'><label>" + MIN_NIGHTS + "</label></div><div style='width:23%;' align='center'><label><input type='text' id = 'rentalRate' value = '" + rate + "'></label></div><div onclick = 'updaterate(" + id + "," + from + "," + to + "," + night + "," + rate + ");' style = 'cursor:pointer'><img class='deletes' src='<?= IMAGES_URL ?>accept.png' width = '20' height = '20'></div><div onclick = 'do_ppty();' style = 'cursor:pointer'><img class='deletes'  src='<?= IMAGES_URL ?>cancel.png' width = '20' height = '20'></div></div>");


        var ratefrom = $("#rateFrom").val();
        ratefrom = ratefrom.split("-");

        var rateto = $("#rateTo").val();
        rateto = rateto.split("-");


        $("#rateFrom").datepicker({minDate: new Date(ratefrom[2], ratefrom[1] - 1, ratefrom[0]),
            maxDate: new Date(rateto[2], rateto[1] - 1, rateto[0])
        });

        $("#rateTo").datepicker({minDate: new Date(ratefrom[2], ratefrom[1] - 1, ratefrom[0]),
            maxDate: new Date(rateto[2], rateto[1] - 1, rateto[0])
        });


    }
    function extras_show() //function for showing extras
    {
        var Data = 0;
        $('#extra_blck').html("<div align = 'center' style= 'clear:both;width:100%;'><img src = '<?= IMAGES_URL ?>black-001-loading.gif' width = '20' height = '20'></div>");
        $.ajax({
            url: "<?= APPLICATION_URL_ADMIN ?>property/getextras/pptyId/<?= $this->pptyId ?>",
            type: "GET",
            async: false,
            success: function(data) {

                data = $.trim(data);
                $("#extra_blck").text("");

                if (data != '0' && data != "" && data != null)
                {
                    var ArrData = data.split("|");
                    if (ArrData.length > 0)
                    {
                        console.log($("#extra_blck")[0]);
                        for (var i = 0; i < ArrData.length; i++)
                        {
                            var mySplit = ArrData[i].split(",");
                            var OptionValue1 = mySplit[0];
                            var OptionValue2 = mySplit[1];
                            var OptionValue3 = mySplit[2];
                            var OptionValue4 = mySplit[3];
                            var OptionValue5 = mySplit[4];

                            if (OptionValue3 == 1)
                                OptionValue3 = "Compulsory";
                            else
                                OptionValue3 = "Optional";


                            if (OptionValue5 == 0)
                                OptionValue5 = "Night";
                            else
                                OptionValue5 = "Stay";


                            $("#extra_blck").append("<div class ='inExtra' style='width:100%;float:left;'><div style='width:33%' align = 'center'><label>" + OptionValue1 + "</label></div><div style='width:20%;' align = 'center'><label >" + OptionValue2 + "</label></div><div style='width:20%;' align='center'><label>" + OptionValue3 + "</label></div><div style='width:20%;' align='center'><label>" + OptionValue5 + "</label></div><a href = '#' style = 'display:block;'><div onclick = 'deleteextras(" + OptionValue4 + ");' ><img class='deletes' src='<?= IMAGES_URL ?>TRASH - EMPTY.png' width = '20' height = '20'></div></a></div>");
                        }


                    }


                }
                else
                {
                    $("#extra_blck").html("<div align='center' style = 'color:red;width:100%;'>No Items to display</div>");

                }
            }

        })

        return false;

    }

    function do_save_extra() //function for saving extras
    {

        var Data = 0;
        $('#extra_blck').html("<div align = 'center' style='width:100%;'><img src = '<?= IMAGES_URL ?>black-001-loading.gif' width = '20' height = '20'></div>");
        if ($("#mws-validate2").valid())
        {
            $.ajax({
                url: "<?= APPLICATION_URL_ADMIN ?>updater/saveextras/pptyId/<?= $this->pptyId ?>",
                type: "POST",
                data: ({extra_name: $("#extra_name").val(), extra_price: $("#extra_price").val(), extra_type: $("#extra_type").val(),
                    stay_type: $("#stay_type").val(), Option: $('#extraOption:checked').val()}),
                async: false,
                success: function(data) {

                    data = $.trim(data);
                    extras_show();
                }

            });
            window.location.href = window.location.href;

        }
        $('#extra_blck').text("");
        return false;

    }

    function deleteextras(id) //function for deleting extras
    {
        alert(id);
        var flag = 0;
        $.ajax({
            url: "<?= APPLICATION_URL_ADMIN ?>property/deleteextras/id/" + id,
            type: "GET",
            async: false,
            success: function(data) {
                data = $.trim(data);
                if (data != "")
                {
                    alert("Record Sucessfully deleted");
                    flag = 1;
                }
            }
        })

        if (flag == 1)
            extras_show();
    }

</script>
<script type="text/javascript">
    // script for rental rates
    $(document).ready(function(e) {
        //**** Following is the date code *****//
        //****        for rental rate     *****//	

        var today = new Date();
        var d = today.getDate();
        var m = today.getMonth();
        var y = today.getFullYear();

        $("#date_from").datepicker({
            minDate: today,
            maxDate: new Date(y + 2, m, d),
            dateFormat: "dd/mm/yy",
            onSelect: function(dateText) {
                dateText = dateText.split("/");
                var $sD = new Date(dateText[2], dateText[1] - 1, dateText[0]);
                $sD.setDate($sD.getDate() + 1);
                $("#date_to").datepicker('option', 'minDate', $sD);
            }
        });


        $("#date_to").datepicker({
            minDate: today,
            maxDate: new Date(y + 2, m, d),
            dateFormat: "dd/mm/yy"
        });



        jQuery.validator.addMethod("dateCheck", function(value, element) {

            var value1 = $("#date_from").val();
            var value2 = $("#date_to").val();


            var valu1 = value1.split("/");
            var valu2 = value2.split("/");

            value1 = new Date(valu1[2], valu1[1], valu1[0]);
            value2 = new Date(valu2[2], valu2[1], valu2[0]);



            return (value2 > value1);

        }, "Choose Proper Date");
        //**** Above is the date code *****//
        //****  for rental rate ends  *****//	

    });


    function cal_default(value)
    {
        var c = confirm("Are you sure you want to proceed??");

        if (c)
        {

            $.ajax({
                type: 'POST',
                url: '<?= APPLICATION_URL_ADMIN ?>property/setcaldefault/pptyId/<?= $this->pptyId ?>',
                async: false,
                data: ({Value: value}),
                success: function(data) {
                    data = $.trim(data);
                    window.location = window.location.href;

                }
            });
        }
        else
            window.location = window.location.href;

    }

    //sript for rental rate ends
</script>
<div>
    <div class="mws-panel grid_8">
        <div class="mws-panel-header"><span class="mws-i-24 i-check">Properties Filtered</span></div>
        <div class="mws-panel-body">
            <ul class="ppty-list-blck-wrap" style="white-space:nowrap;overflow:scroll;">
            <?php
                foreach($this->propertyList as $pptyVal){
            ?>  
                <li class="ppty-list-blck" href="<?=APPLICATION_URL_ADMIN?>dashboard#customer" style="position:relative;">
                    <span class="mws-report-value" style="font-size:16px;margin:0 auto;"><?=$pptyVal['propertycode'];?></span>
                </li>
            <?php
                }
            ?>
            </ul>
        </div>
    </div>
    <div class="mws-panel grid_8">
        <div class="mws-panel-header"><span class="mws-i-24 i-check">Specification</span></div>
        <div class="mws-panel-body" >
            <fieldset id="step-2" class="mws-form-inline" >
                <div style="padding:20px;">
                    <h4>Add or Overwrite</h4>
                    <div class="mws-form">
                        <div class="mws-form-inline">
                            <div class="mws-form-row">
                                <label><input type="radio" name="add" id="add" value="add" checked="checked" onclick="updaterCategorize('add');">Add</label>
                                <label><input type="radio" name="add" id="overwrite" value="overwrite" onclick="updaterCategorize('overwrite');">Overwrite</label>
                            </div>
                        </div>
                    </div>
                    <?
                    $i = 0;
                    $cat_name = "";
                    $cat_name1 = "";
                    $form_flag = 0;

                    foreach ($this->specData as $value) /* loop of displaying property specification */ {
                        if ($value['mandatory']) /* checking mandatory fields */
                            $mandatory = "<span class = 'red'>*</span>";
                        else
                            $mandatory = "";



                        if ($form_flag == 1 && $cat_name != $value['cat_name']) {
                            ?>
                            <div class="mws-form-inline"><div class="mws-form-row">
                                    <input type="submit"  class = "mws-button red" value ="Save progress"/>

                                </div></div>
                            <?= $this->myform->step ?>
                            <input type="hidden" name="addUpdater" class="addUpdater" value="add" >
                            </form>
                            <?
                        }


                        if ($cat_name != $value['cat_name']) {
                            echo '<form name="myform' . $i . '" id="mws-validate' . $i . '" method="post"  action = "'.(APPLICATION_URL_ADMIN).'updater/submit" novalidate="novalidate" class="mws-form" >';
                            echo "<h4>" . $value['cat_name'] . "</h4>";
                            $form_flag = 1;
                        }
                        $cat_name = $value['cat_name'];
                        $x = "ques" . $i;

                        if ($value['spec_id'] != BATHROOM_ID && $value['spec_id'] != BEDROOM_ID && $value['spec_id'] != ADDITIONAL_BATHROOM_ID) {
                            ?> 
                            <label><?= $value['question'] . $mandatory ?></label>
                            <br /><br />

                            <!-- <div style = 'padding:5px;margin-bottom:20px;width:100%;height:auto;' class = 'ppty_spec_block'>-->
                            <div class="mws-form-inline"><div class="mws-form-row">
                                    <?= $this->myform->$x ?>
                                    <label for="<?= $x . "[]" ?>" class="error" style="display:none;">Please choose one.</label>
                                </div></div>
                            <?
                        } else {
                            for ($s = 0; $s < $this->no_bedrooms; $s++) {

                                $tmp = "ques" . $i . $s;


                                if ($s == 0) {
                                    ?>			

                                    <label><?= $value['question'] . $mandatory ?></label>
                                    <br /><br />
                                    <?
                                }
                                ?>
                                <!-- <div style = 'padding:5px;margin-bottom:20px;width:100%;height:auto;' class = 'ppty_spec_block'>-->
                                <div class="mws-form-inline"><div class="mws-form-row">
                                        <span style="font-weight:bold;margin-bottom:30px;">For Bedroom  <?= $s + 1 ?></span><br /><?= $this->myform->$tmp ?>
                                        <label for="<?= $x . "[]" ?>" class="error" style="display:none;">Please choose one.</label>
                                    </div></div>


                                <?
                                $i++;
                            }// end for loop
                            $i--;
                        }
                        $i++;
                    }
                    ?>
                    <div class="mws-form-inline"><div class="mws-form-row">
                            <input type="submit"  class = "mws-button red" value ="save"/>

                        </div></div>
                    <?= $this->myform->step ?>
                    <input type="hidden" name="addUpdater" class="addUpdater" value="add" >
                </div> 
                </form>
            </fieldset>
        </div>
    </div>
</div>
<script type="text/javascript">
    function updaterCategorize(category){
        console.log(category);
        if(category == "add"){
            $(".addUpdater").val("add");
        }else{
            $(".addUpdater").val("overwrite");
        }
    }
</script>